package com.cowcowit.springcloud.controller;

import com.cowcowit.springcloud.entity.Account;
import com.cowcowit.springcloud.result.ResultData;
import com.cowcowit.springcloud.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  账户控制器
 * </p>
 *
 * @author whboy
 * @since 2025-09-23
 */
@RestController
@RequestMapping("/springcloud/account")
public class AccountController {
    
    @Autowired
    private IAccountService accountService;

    // 扣减账户余额
    @RequestMapping("/decrease")
    public ResultData decrease(@RequestParam("userId") Long userId, @RequestParam("money") Long money){
        accountService.decrease(userId,money);
        return ResultData.success("扣减账户余额成功！");
    }

    /**
     * 新增账户记录
     * @param account 账户对象
     * @return 操作结果
     */
    @PostMapping("/add")
    public ResultData<String> addAccount(@RequestBody Account account) {
        boolean result = accountService.save(account);
        if (result) {
            return ResultData.success("账户添加成功");
        } else {
            return ResultData.fail("500", "账户添加失败");
        }
    }
    
    /**
     * 根据ID删除账户记录
     * @param id 账户ID
     * @return 操作结果
     */
    @DeleteMapping("/delete/{id}")
    public ResultData<String> deleteAccountById(@PathVariable("id") Long id) {
        boolean result = accountService.removeById(id);
        if (result) {
            return ResultData.success("账户删除成功");
        } else {
            return ResultData.fail("500", "账户删除失败");
        }
    }
    
    /**
     * 更新账户记录
     * @param account 账户对象
     * @return 操作结果
     */
    @PostMapping("/update")
    public ResultData<String> updateAccount(@RequestBody Account account) {
        boolean result = accountService.updateById(account);
        if (result) {
            return ResultData.success("账户更新成功");
        } else {
            return ResultData.fail("500", "账户更新失败");
        }
    }
    
    /**
     * 根据ID获取账户信息
     * @param id 账户ID
     * @return 账户信息
     */
    @GetMapping("/get/{id}")
    public ResultData<Account> getAccountById(@PathVariable("id") Long id) {
        Account account = accountService.getById(id);
        return ResultData.success(account);
    }
    
    /**
     * 查询所有账户记录
     * @return 所有账户记录列表
     */
    @GetMapping("/list")
    public ResultData<List<Account>> getAllAccounts() {
        List<Account> accounts = accountService.list();
        return ResultData.success(accounts);
    }
}